package me.firstapp.manage.marketing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.manage.utils.BizData4Page;
import me.firstapp.module.weixin.system.Banner;
import me.firstapp.module.weixin.system.Broadcast;
import me.firstapp.service.weixin.system.BannerService;
import me.firstapp.service.weixin.system.BroadcastService;

@Controller
@RequestMapping(value = "/marketingManage")
public class MarketingManageAct extends BaseManage {

	@Autowired
	private BannerService bannerService;

	@Autowired
	private BroadcastService broadcastService;

	@ResponseBody
	@RequestMapping(value = "/findBannerPage.do", method = RequestMethod.POST)
	public void findBannerPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				Page<Banner> bannerPage = bannerService.findBannerPage(pageNo, iDisplayLength, searchParams, mDataProp,
						sSortDir_0);

				BizData4Page<Banner> bizData4Page = new BizData4Page<Banner>();
				bizData4Page.setUserdata(conditions);
				if (bannerPage != null) {
					bizData4Page.setPage(bannerPage.getPageNo());
					bizData4Page.setPagesize(bannerPage.getPageSize());
					bizData4Page.setRecords(bannerPage.getEntityCount());
					bizData4Page.setRows(new ArrayList<Banner>(bannerPage.getEntities()));
					bizData4Page.setTotal(bannerPage.getEntityCount());
				}

				result.setObject(bizData4Page);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@ResponseBody
	@RequestMapping(value = "/findBroadcastPage.do", method = RequestMethod.POST)
	public void findBroadcastPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				Page<Broadcast> broadcastPage = broadcastService.findBroadcastPage(pageNo, iDisplayLength, searchParams,
						mDataProp, sSortDir_0);

				BizData4Page<Broadcast> bizData4Page = new BizData4Page<Broadcast>();
				bizData4Page.setUserdata(conditions);
				if (broadcastPage != null) {
					bizData4Page.setPage(broadcastPage.getPageNo());
					bizData4Page.setPagesize(broadcastPage.getPageSize());
					bizData4Page.setRecords(broadcastPage.getEntityCount());
					bizData4Page.setRows(new ArrayList<Broadcast>(broadcastPage.getEntities()));
					bizData4Page.setTotal(broadcastPage.getEntityCount());
				}

				result.setObject(bizData4Page);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/addBanner.do", method = RequestMethod.POST)
	public void addBanner(String tag, String name, String imageUrl, String url, String remark, Integer sort,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			bannerService.executeAdd(tag, name, imageUrl, url, remark, sort);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateBanner.do", method = RequestMethod.POST)
	public void updateBanner(Long id, String tag, String name, String imageUrl, String url, String remark, Integer sort,
			Integer status, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			bannerService.executeUpdate(id, tag, name, imageUrl, url, remark, sort, status);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/addBroadcast.do", method = RequestMethod.POST)
	public void addBroadcast(String type, String name, String backgroundColor, String url, Integer sort, String remark,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			broadcastService.executeAdd(type, name, backgroundColor, url, sort, remark);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateBroadcast.do", method = RequestMethod.POST)
	public void updateBroadcast(Long id, String type, String name, String backgroundColor, String url, Integer sort,
			Integer status, String remark, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			broadcastService.executeUpdate(id, type, name, backgroundColor, url, sort, status, remark);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
